package com.credithc.signature.common.service;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.mapper.SignatureLogMapper;
import com.credithc.signature.common.po.SignatureLog;
import com.credithc.signature.common.ro.manager.JobRequestRo;
import com.credithc.signature.common.utils.PageUtils;
import com.credithc.signature.common.vo.manager.RequestLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年06⽉29⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Service
public class JobLogService {

    private static ThreadLocal<StopWatch> localStopWatch = ThreadLocal.withInitial(() -> new StopWatch());

    @Autowired
    SignatureLogMapper signatureLogMapper;

    @Autowired
    SignatureLogService signatureLogService;

    /**
     * 查询所有【处理中】和【已处理合同】
     * 0：未处理 1：处理中 2：处理成功 3：处理失败
     *
     * @param jobRequestRo
     * @return
     */
    public PageInfo<RequestLogVo> queryAll(JobRequestRo jobRequestRo) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {
        // 1、查询
        PageUtils.setPageNo(jobRequestRo);
        StopWatch stopWatch = localStopWatch.get();
        stopWatch.start();
        int count = signatureLogMapper.selectJobInfosCount(jobRequestRo);
        List<SignatureLog> signatureLogs = signatureLogMapper.selectJobInfos(jobRequestRo);
        stopWatch.stop();
        double seconds = stopWatch.getTotalTimeSeconds();
        localStopWatch.remove();
        // 2、封装返回参数
        List<RequestLogVo> requestLogVos = signatureLogService.buildPos(signatureLogs);
        log.info("<===== JobRequestLog 查询耗时 : {}秒", seconds);
        return new PageInfo<>(requestLogVos, count, jobRequestRo.getPageNo(), jobRequestRo.getPageSize());
    }
}
